{% extends "base.html" %}

{% block title %}检查用户依赖项 - {{ user.username }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-exclamation-triangle text-warning me-2"></i>
                        检查用户依赖项 - {{ user.username }}
                    </h5>
                </div>
                <div class="card-body">
                    <div class="alert alert-warning">
                        <h6><i class="fas fa-info-circle me-2"></i>重要提示</h6>
                        <p class="mb-0">
                            用户 <strong>{{ user.username }}</strong> 存在以下依赖项，删除前需要处理这些数据。
                            您可以选择将依赖项转移给其他用户，或者直接删除用户及其所有相关数据。
                        </p>
                    </div>

                    <!-- 依赖项列表 -->
                    <div class="row">
                        <div class="col-12">
                            <h6 class="mb-3"><i class="fas fa-list me-2"></i>依赖项详情</h6>
                            
                            {% if dependencies.dingtalk_robots %}
                            <div class="mb-4">
                                <h6 class="text-primary">
                                    <i class="fas fa-robot me-2"></i>钉钉机器人 ({{ dependencies.dingtalk_robots|length }}个)
                                </h6>
                                <div class="table-responsive">
                                    <table class="table table-sm table-bordered">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>名称</th>
                                                <th>标签</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for item in dependencies.dingtalk_robots %}
                                            <tr>
                                                <td>{{ item.id }}</td>
                                                <td>{{ item.name }}</td>
                                                <td>{{ item.tag }}</td>
                                            </tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            {% endif %}

                            {% if dependencies.dingtalk_tasks %}
                            <div class="mb-4">
                                <h6 class="text-primary">
                                    <i class="fas fa-clock me-2"></i>钉钉任务 ({{ dependencies.dingtalk_tasks|length }}个)
                                </h6>
                                <div class="table-responsive">
                                    <table class="table table-sm table-bordered">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>名称</th>
                                                <th>Cron表达式</th>
                                                <th>状态</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for item in dependencies.dingtalk_tasks %}
                                            <tr>
                                                <td>{{ item.id }}</td>
                                                <td>{{ item.name }}</td>
                                                <td>{{ item.cron_expression }}</td>
                                                <td>{{ item.status }}</td>
                                            </tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            {% endif %}

                            {% if dependencies.custom_scripts %}
                            <div class="mb-4">
                                <h6 class="text-primary">
                                    <i class="fas fa-file-code me-2"></i>自定义脚本 ({{ dependencies.custom_scripts|length }}个)
                                </h6>
                                <div class="table-responsive">
                                    <table class="table table-sm table-bordered">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>名称</th>
                                                <th>文件名</th>
                                                <th>大小</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for item in dependencies.custom_scripts %}
                                            <tr>
                                                <td>{{ item.id }}</td>
                                                <td>{{ item.name }}</td>
                                                <td>{{ item.filename }}</td>
                                                <td>{{ item.file_size }} bytes</td>
                                            </tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            {% endif %}

                            {% if dependencies.custom_tasks %}
                            <div class="mb-4">
                                <h6 class="text-primary">
                                    <i class="fas fa-tasks me-2"></i>自定义任务 ({{ dependencies.custom_tasks|length }}个)
                                </h6>
                                <div class="table-responsive">
                                    <table class="table table-sm table-bordered">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>名称</th>
                                                <th>Cron表达式</th>
                                                <th>状态</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for item in dependencies.custom_tasks %}
                                            <tr>
                                                <td>{{ item.id }}</td>
                                                <td>{{ item.name }}</td>
                                                <td>{{ item.cron_expression }}</td>
                                                <td>{{ item.last_run_status or '未运行' }}</td>
                                            </tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            {% endif %}

                            {% if dependencies.background_tasks %}
                            <div class="mb-4">
                                <h6 class="text-primary">
                                    <i class="fas fa-cogs me-2"></i>后台任务 ({{ dependencies.background_tasks|length }}个)
                                </h6>
                                <div class="table-responsive">
                                    <table class="table table-sm table-bordered">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>包名</th>
                                                <th>操作</th>
                                                <th>状态</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for item in dependencies.background_tasks %}
                                            <tr>
                                                <td>{{ item.id }}</td>
                                                <td>{{ item.package_name }}</td>
                                                <td>{{ item.operation }}</td>
                                                <td>{{ item.status }}</td>
                                            </tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            {% endif %}

                            {% if dependencies.package_logs %}
                            <div class="mb-4">
                                <h6 class="text-primary">
                                    <i class="fas fa-history me-2"></i>包安装日志 ({{ dependencies.package_logs|length }}条)
                                </h6>
                                <div class="table-responsive">
                                    <table class="table table-sm table-bordered">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>包名</th>
                                                <th>操作</th>
                                                <th>状态</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for item in dependencies.package_logs %}
                                            <tr>
                                                <td>{{ item.id }}</td>
                                                <td>{{ item.package_name }}</td>
                                                <td>{{ item.operation }}</td>
                                                <td>{{ item.status }}</td>
                                            </tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            {% endif %}
                        </div>
                    </div>

                    <!-- 操作选项 -->
                    <div class="row mt-4">
                        <div class="col-12">
                            <h6 class="mb-3"><i class="fas fa-cog me-2"></i>处理选项</h6>
                            
                            <!-- 转移依赖项 -->
                            <div class="card mb-3">
                                <div class="card-header">
                                    <h6 class="mb-0">
                                        <i class="fas fa-exchange-alt text-success me-2"></i>
                                        选项1：转移依赖项给其他用户
                                    </h6>
                                </div>
                                <div class="card-body">
                                    <form method="POST" action="{{ url_for('transfer_user_dependencies', user_id=user.id) }}">
                                        <input type="hidden" name="action" value="transfer">
                                        <div class="row">
                                            <div class="col-md-6">
                                                <label for="heir_id" class="form-label">选择继承人：</label>
                                                <select class="form-select" id="heir_id" name="heir_id" required>
                                                    <option value="">请选择继承人...</option>
                                                    {% for heir in target_users %}
                                                    <option value="{{ heir.id }}">
                                                        {{ heir.username }} 
                                                        {% if heir.is_admin %}(管理员){% endif %}
                                                    </option>
                                                    {% endfor %}
                                                </select>
                                                <div class="form-text">
                                                    选择将依赖项转移给哪个用户。转移后，所有相关数据将归属于新用户。
                                                </div>
                                            </div>
                                            <div class="col-md-6 d-flex align-items-end">
                                                <button type="submit" class="btn btn-success">
                                                    <i class="fas fa-exchange-alt me-2"></i>
                                                    转移依赖项并删除用户
                                                </button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>

                            <!-- 直接删除 -->
                            <div class="card">
                                <div class="card-header">
                                    <h6 class="mb-0">
                                        <i class="fas fa-trash text-danger me-2"></i>
                                        选项2：直接删除用户及其所有数据
                                    </h6>
                                </div>
                                <div class="card-body">
                                    <div class="alert alert-danger">
                                        <h6><i class="fas fa-exclamation-triangle me-2"></i>警告</h6>
                                        <p class="mb-0">
                                            此操作将永久删除用户 <strong>{{ user.username }}</strong> 及其所有相关数据，
                                            包括钉钉机器人、任务、脚本、日志等。此操作不可撤销！
                                        </p>
                                    </div>
                                    <form method="POST" action="{{ url_for('transfer_user_dependencies', user_id=user.id) }}" 
                                          onsubmit="return confirm('确定要删除用户 {{ user.username }} 及其所有数据吗？此操作不可撤销！')">
                                        <input type="hidden" name="action" value="delete_without_transfer">
                                        <button type="submit" class="btn btn-danger">
                                            <i class="fas fa-trash me-2"></i>
                                            确认删除用户及所有数据
                                        </button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 返回按钮 -->
                    <div class="row mt-4">
                        <div class="col-12">
                            <a href="{{ url_for('user_management') }}" class="btn btn-secondary">
                                <i class="fas fa-arrow-left me-2"></i>
                                返回用户管理
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
// 页面加载完成后的初始化
document.addEventListener('DOMContentLoaded', function() {
    // 为转移按钮添加确认
    const transferForm = document.querySelector('form[action*="transfer_user_dependencies"]');
    if (transferForm) {
        transferForm.addEventListener('submit', function(e) {
            const heirSelect = document.getElementById('heir_id');
            if (!heirSelect.value) {
                e.preventDefault();
                alert('请选择继承人！');
                return false;
            }
            
            const heirName = heirSelect.options[heirSelect.selectedIndex].text;
            if (!confirm(`确定要将 {{ user.username }} 的所有依赖项转移给 ${heirName} 吗？`)) {
                e.preventDefault();
                return false;
            }
        });
    }
});
</script>
{% endblock %}